package cn.ilikes.tools.nosql.jpa.persistences.contexts.operations;

import cn.ilikes.tools.nosql.jpa.clients.AbClient;
import cn.ilikes.tools.nosql.jpa.clients.JPASQLClientsException;
import cn.ilikes.tools.nosql.jpa.graphs.Node;
import cn.ilikes.tools.nosql.jpa.metadatas.EntityMetaData;
import cn.ilikes.tools.nosql.jpa.persistences.contexts.AbstractContext;
import cn.ilikes.tools.nosql.jpa.persistences.contexts.Operation;
import cn.ilikes.tools.nosql.jpa.persistences.contexts.OperationRunTimeException;

public class ReferenceOperation extends ContextOperation implements Operation<Node> {

	private Object id;

	private Class<?> entity;

	public ReferenceOperation(AbstractContext context, Object id, Class<?> entity) {
		super(OperationType.reference, context);
		this.id = id;
		this.entity = entity;

	}

	@Override
	public Node exec() {
		try {
			AbstractContext context = getContext();
			AbClient client = context.getClient();
			EntityMetaData entityMetaData = context.getPersistenUnitEnv().getMetaDataManagerFactory().selectIEntityMetaData(entity);
			return client.load(entityMetaData, id);
		} catch (JPASQLClientsException e) {
			throw new OperationRunTimeException(e);
		}
	}

}
